package com.inossem.warehousestorage_inossem.activity.query;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.query.extra.QueryExtra;
import com.inossem.warehousestorage_inossem.adapter.query.QueryMaterielListAdapter;
import com.inossem.warehousestorage_inossem.bean.query.QueryMaterielBean;
import com.inossem.warehousestorage_inossem.bean.query.QueryMaterielListRequestBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.query.QueryMaterielListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

//转储管理物料列表
public class QueryBatchMaterielListActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView materielList;

    private List<QueryMaterielBean.DataBean.MaterielListBean> mMaterielList;

    private QueryMaterielListAdapter adapter;

    private String mMaterielCode, mMaterielDescription, mPositionNumber, mWarehouseNumber, mAreaNumber, mCondition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_storage_materiel_list);
        initView(QueryBatchMaterielListActivity.this);
        initData(QueryBatchMaterielListActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        materielList = (PullableListView) activity.findViewById(R.id.materielList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.materielListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_query_batch_inventory_select_bulk_inventory_enquiries));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_query_batch_materiel_list_batch_query), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        materielList.setPullUpFlag(false);
        materielList.setPullDownFlag(false);
        getExtra(activity);
        getData(activity, mWarehouseNumber, mAreaNumber, mMaterielCode, mMaterielDescription, mPositionNumber);
    }

    private void getExtra(Activity activity) {
        Intent intent = activity.getIntent();
        int conditionType = intent.getIntExtra(QueryExtra.CONDITION_TYPE, QueryBatchInventorySelectActivity.MATERIEL_DESCRIPTION);
        switch (conditionType) {
            case QueryBatchInventorySelectActivity.MATERIEL_DESCRIPTION:
                mMaterielDescription = intent.getStringExtra(QueryExtra.CONDITION);
                break;
            case QueryBatchInventorySelectActivity.MATERIEL_CODE:
                mMaterielCode = intent.getStringExtra(QueryExtra.CONDITION);
                break;
            case QueryBatchInventorySelectActivity.POSITION_NUMBER:
                mPositionNumber = intent.getStringExtra(QueryExtra.CONDITION);
                break;
        }
        mWarehouseNumber = intent.getStringExtra(QueryExtra.WAREHOUSE_NUMBER);
        mAreaNumber = intent.getStringExtra(QueryExtra.AREA_NUMBER);
    }

    private void addMaterielListData(Context context, List<QueryMaterielBean.DataBean.MaterielListBean> materielList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new QueryMaterielListAdapter(context, materielList, mCondition, (String condition) -> {
                mCondition = condition;
                addMaterielListData(context, filterMateriel(mCondition), listView, true);
            });
            listView.setAdapter(adapter);
        }
    }

    private List<QueryMaterielBean.DataBean.MaterielListBean> filterMateriel(String filterCondition) {
        if (TextUtils.isEmpty(filterCondition)) {
            return mMaterielList;
        }
        List<QueryMaterielBean.DataBean.MaterielListBean> result = new ArrayList<>();
        for (int i = 0; i < mMaterielList.size(); i++) {
            QueryMaterielBean.DataBean.MaterielListBean materielItem = mMaterielList.get(i);
            if (materielItem.getMaterielCode().contains(filterCondition) || materielItem.getMaterielDescription().contains(filterCondition)) {
                result.add(materielItem);
                continue;
            }
        }
        return result;
    }

    private void getData(Context context, String warehouseNumber, String areaNumber, String materielCode, String materielDescription, String positionNumber) {
        QueryMaterielListRequest.request(context, getQueryMaterielListRequestBean(warehouseNumber, areaNumber, materielCode, materielDescription, positionNumber),
                new GsonBeanListener<QueryMaterielBean>() {
                    @Override
                    public void onSuccess(QueryMaterielBean bean) {
                        if (bean.getData().getMaterielList().isEmpty()) {
                            Utils.middleToast(context, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                        } else {
                            mMaterielList = bean.getData().getMaterielList();
                            addMaterielListData(context, ActivityUtils.getShowData(mMaterielList, 0), materielList, true);
                        }
                    }
                });
    }

    public interface BatchMaterielListener {
        void onNoDoubleClickListener(String condition);
    }

    private QueryMaterielListRequestBean getQueryMaterielListRequestBean(String warehouseNumber, String areaNumber, String materielCode, String materielDescription, String positionNumber) {
        QueryMaterielListRequestBean result = new QueryMaterielListRequestBean();
        result.setWarehouseNumber(warehouseNumber == null ? "" : warehouseNumber);
        result.setAreaNumber(areaNumber == null ? "" : areaNumber);
        result.setMaterielCode(materielCode == null ? "" : materielCode);
        result.setMaterielDescription(materielDescription == null ? "" : materielDescription);
        result.setPositionNumber(positionNumber == null ? "" : positionNumber);
        return result;
    }

}
